--
-- Author: mm
-- Date: 2018-04-05 16:30:28
--
-- LionHornRoundEndView
--

local LionHornRewardRender = import( ".renders.LionHornRewardRender" )

local fileName = string.getModeName(...)
local ClassRef = class(fileName, app.mvc.ViewBase)

local maxRewardNum = 5 -- 奖励最多个数
local rewardDistanceOfX = 0 -- 奖励X间距
local rewardMiddlePosX = 0 --  奖励居中X坐标
local rewardWidth = 0 -- 奖励panel宽度

ClassRef.VIEW_ROOT_RES = Res.LionRoundEndUi

function ClassRef:init()
	local bg = self._ui.bg
	local size = bg:getContentSize()
	size.width = display.width
	bg:setContentSize(size)

	self._rewardList = {}
	for i = 1, maxRewardNum do
		local rewardTemp = LionHornRewardRender.create( self._ui["icon_" .. i] )
		self._rewardList[ i ] = rewardTemp
	end

	rewardDistanceOfX = self._rewardList[ 2 ]:getPositionX() - self._rewardList[ 1 ]:getPositionX()
	rewardMiddlePosX = self._rewardList[ 3 ]:getPositionX()
	rewardWidth = self._rewardList[ 1 ]:getContentSize2()
end

-- data = message S2C_LionHeartHornRoundEnd {
-- 	repeated LionHeartHornMonsterInfo lionHeartHornMonsterInfos = 1;//回合击杀列表
-- 	int32 round
-- 	bool isMaxRewardTimes = 3;//是否达到奖励最大次数
-- }
function ClassRef:refreshContent( dataTemp )
	if dataTemp.isMaxRewardTimes then
		self._ui.Panel_1:setVisible(false)
		self._ui.Node_tips:setVisible(true)
		self._ui.Node_tips:findChild("Sprite/name"):setString(string.format(L("lua_code_text_464"), dataTemp.round))

		self:performWithDelay(function()
			executeFunc( self._onCloseView )
			executeFunc( self._onOperate, "roundRwardEnd" )
		end, 1 + 20/60 + 2)	 -- 动画播放完成之后延迟2秒开始下一回合
		return
	end
	self._ui.Panel_1:setVisible(true)
	self._ui.Node_tips:setVisible(false)
	self._ui.tlt:setString( string.format( L("lua_code_text_464"), dataTemp.round ) )

	--if dataTemp.isMaxRewardTimes then -- 已经达到奖励最大次数
		--self._ui.tlt_kill:setString( L("ErrEventRewardNotEnough") )
	--else
		self._ui.tlt_kill:setVisible(false)
		self._ui.tlt_kill:setString( L("lua_code_text_17") )


		local function sortFunc( lhs, rhs )
			local lionRewardConf_1 = GD:getLionRewardById( lhs.monsterID )
			local rewardType_1 = tonumber( lionRewardConf_1.type )

			local lionRewardConf_2 = GD:getLionRewardById( rhs.monsterID )
			local rewardType_2 = tonumber( lionRewardConf_2.type )

			if rewardType_1 == rewardType_2 then
				return tonumber( lionRewardConf_1.ID ) < tonumber( lionRewardConf_2.ID )
			else
				return rewardType_1 < rewardType_2
			end
		end
		table.sort( dataTemp.lionHeartHornMonsterInfos, sortFunc )
		local rewardNum = #dataTemp.lionHeartHornMonsterInfos
		self:_adjustRewardPos( rewardNum )
		for i = 1, maxRewardNum do
			local itemDataTemp = nil
			if i <= rewardNum then
				itemDataTemp = dataTemp.lionHeartHornMonsterInfos[i]
			end
			self._rewardList[ i ]:setData( { itemDataTemp, dataTemp.isMaxRewardTimes } )
		end
	--end

	self:performWithDelay(function()
		self._ui.tlt_kill:setVisible(true)
		self:playAction( nil, false )
	end, 1 + 20/60)

	self:performWithDelay(function()
		executeFunc( self._onCloseView )
		executeFunc( self._onOperate, "roundRwardEnd" )
	end, 1 + 20/60 + 2)	 -- 动画播放完成之后延迟2秒开始下一回合
end

-- 调整奖励位置居中
function ClassRef:_adjustRewardPos( rewardNum )
	if 0 == rewardNum%2 then -- 偶数个击杀记录
		local leftMid = rewardNum/2 -- 左侧标记item
		local leftMidPosX = rewardMiddlePosX - ( rewardWidth + rewardDistanceOfX ) / 2 -- 左侧标记item坐标X
		self._rewardList[ leftMid ]:setPositionX( leftMidPosX )

		for i = leftMid - 1, 1, -1 do
			local posTemp = leftMidPosX - ( rewardWidth + rewardDistanceOfX ) * ( leftMid - i )
			self._rewardList[ i ]:setPositionX( posTemp )
		end

		local rightMid = rewardNum/2 + 1 -- 右侧标记item
		local rightMidPosX = rewardMiddlePosX + ( rewardWidth + rewardDistanceOfX ) / 2 -- 右侧标记item坐标X
		self._rewardList[ rightMid ]:setPositionX( rightMidPosX )
		for i = rightMid + 1, rewardNum do
			local posTemp = rightMidPosX + ( rewardWidth + rewardDistanceOfX ) * ( i - rightMid )
			self._rewardList[ i ]:setPositionX( posTemp )
		end
	else
		local midNum = ( rewardNum + 1 ) / 2
		self._rewardList[ midNum ]:setPositionX( rewardMiddlePosX )
		for i = midNum - 1, 1, -1 do
			local posTemp = rewardMiddlePosX - ( rewardWidth/2 + rewardDistanceOfX ) * ( midNum - i )
			self._rewardList[ i ]:setPositionX( posTemp )
		end
		for i = midNum + 1, rewardNum do
			local posTemp = rewardMiddlePosX + ( rewardWidth/2 + rewardDistanceOfX ) * ( i - midNum )
			self._rewardList[ i ]:setPositionX( posTemp )
		end
	end
end

return ClassRef
